package com.frain.springcloud.util;

import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class FutureUtil {
    protected static final Logger logger = LoggerFactory.getLogger(FutureUtil.class.getName());

    private static final Integer DEFAULT_THREAD_NUM = 6;

    private static final Integer DEFAULT_MAP_NUM = 8;

    static ExecutorService executorService = Executors.newFixedThreadPool(DEFAULT_THREAD_NUM);

    private static volatile Map<String, Future<String>> futureMap = new ConcurrentHashMap<String, Future<String>>(
            DEFAULT_MAP_NUM);

    /**
     * 
     * @Title: getHttpFuture 
     * @Description:创建http请求
     * @param requestObject
     * @return TODO(说明)
     * @throws 
     * @author xuyp
     * @date 2018年6月30日 上午9:54:51
     */
    public static Future<String> sendHttp(final HttpRequestObject requestObject) {

        logger.info("send http request :" + requestObject.toString());

        if (requestObject == null || StringUtils.isBlank(requestObject.getUrl()))
            return futureMap.get(requestObject.toString());

        synchronized (futureMap) {

            if (futureMap.containsKey(requestObject.toString())) {

                return futureMap.get(requestObject.toString());
            } else {
                Future<String> future = executorService.submit(new Callable<String>() {

                    @Override
                    public String call() throws Exception {

                        return HttpRequest.send(requestObject);
                    }

                });

                futureMap.put(requestObject.toString(), future);

                return future;
            }

        }

    }

    /**
     * 
     * @Title: getHttpResult 
     * @Description: 异步返回请求结果 
     * @param requestObject
     * @return 
     * @throws 
     * @author xuyp
     * @date 2018年6月30日 上午9:55:08
     */
    public static String getHttpResult(HttpRequestObject requestObject) {

        String result = "";

        try {

            if (requestObject != null && futureMap.containsKey(requestObject.toString())) {

                Future<String> future = futureMap.get(requestObject.toString());
                futureMap.remove(requestObject.toString());
                result = future.get();
                logger.info("请求参数requestObject=" + requestObject + "        result=" + result);
            }

        }
        catch (Exception e) {

            result = e.getMessage();
        }

        logger.info(requestObject.toString() + "返回调用结果" + result);

        return result;

    }

    /**
     * 
     * @Title: removeFuture 
     * @Description: 移除返回结果列表
     * @param requestObject TODO(说明)
     * @throws 
     * @author xuyp
     * @date 2018年6月30日 上午9:55:26
     */
    public static void removeFuture(HttpRequestObject requestObject) {

        if (futureMap.containsKey(requestObject.toString())) {
            futureMap.remove(requestObject.toString());
        }

    }

    public static void main(String[] args) {
        System.out.println(sendHttp(HttpRequestObjectBuilder.newInstance().builder()));
    }
}